package com.lk.common.utils.excel.thread;

import java.util.concurrent.ArrayBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lk.common.utils.excel.entity.Flag;
import com.lk.common.utils.excel.entity.Target;

public class WriteThread implements Runnable {
	
	private static final Logger logger = LoggerFactory.getLogger(WriteThread.class);
	
	private ArrayBlockingQueue<Target> queue;
	private volatile Flag flag;
	
	public WriteThread(ArrayBlockingQueue<Target> queue,Flag flag) {
		this.queue = queue;
		this.flag = flag;
	}

	@Override
	public void run() {
		while(true) {
			try {
				if(queue.isEmpty()) {
					synchronized (this) {
						if(queue.isEmpty() && flag.isReadDone()) {
							break;	//读取完成
						}
					}
				}
				Target target = queue.take();
				System.out.println(target);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		logger.debug("线程读取完成！");
	}
}
